package cn.taobaoke.restful.controller.admin;

import cn.taobaoke.restful.controller.BaseController;
import cn.taobaoke.restful.domain.AccountsEntity;
import cn.taobaoke.restful.exception.TBKException;
import cn.taobaoke.restful.interceptor.Auth;
import cn.taobaoke.restful.service.AccountsService;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

/**
 * BalanceController类
 *
 * @author luchuan
 * @doc
 * @date 17/6/1
 */
@RestController
@RequestMapping("/{domain}/{domainId}/api/admin/balance")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class AdminBalanceEndpoint extends BaseController {

    @GetMapping("/agents/accounts")
    public Result getAgentAccounts(@PathVariable Long domainId, @PathVariable String domain, String keyword, Integer page, Integer size) {
//        Pageable pageable = createPageable(page, size);
        keyword = keyword == null ? "" : keyword;
        // TODO 处理代理分组, 交给前端处理
//        Page<AccountsEntity> accountsPage = accountsService.getAdminAgentAccounts(keyword, pageable);
        List<AccountsEntity> accounts = accountsService.getAdminAgentAccounts(keyword, domainId, domain);
        return wrapObj(accounts);
    }

    @PostMapping("/{accountsId}/pay")
    public Result setRealAccount(@PathVariable Long domainId, @PathVariable String domain, @PathVariable Long accountsId, @RequestBody Map map) {
        Object realAccountsObj = map.get("realAccounts");
        Double realAccounts = null;
        if (realAccountsObj instanceof Integer) {
            realAccounts = (Integer) realAccountsObj + 0.0;
        } else {
            realAccounts = (Double) realAccountsObj;
        }
        if (realAccounts == null || realAccounts < 0) {
            throw new TBKException("实际结算不能为空, 必须大于0");
        }
        accountsService.payAccounts(accountsId, realAccounts, domainId, domain);
        return wrapVoid();
    }

    @GetMapping("/stats/excel")
    @Auth(type = Auth.AuthType.ADMIN)
    public void exportStatistics(@PathVariable Long domainId, @PathVariable String domain) {
        Workbook workbook = accountsService.exportAccountStats(domainId, domain);
        try {
            OutputStream out = response.getOutputStream();
            response.reset();
            response.setContentType("APPLICATION/OCTET-STREAM");
            response.setHeader("Content-Disposition", "attachment; filename=statistics.xls");

            workbook.write(out);
            out.close();
        } catch (IOException e) {
            logger.error(e);
        }
    }

    private final @NonNull AccountsService accountsService;

    private static Logger logger = LogManager.getLogger(AdminBalanceEndpoint.class);
}

